from pymongo import MongoClient
import statistics
client = MongoClient(port=27017)
db=client['tiktok']
collist=['homeimprovement','gamingtiktok','gamergoals','imbusyrightnow','90saesthetic','feelinggood','foodie','albumcover','groupchat','beautyhack','autismawareness','guitartok','gettheretogether','hyperfixated'
    ,'greenscreenscan','mobilegame','foodontiktok','ecohacks','defrosting','fitnessroutine','galentinesday','inspirationalquotes','fetapasta','happyholi','colorblast','blackandproud','dinnerparty','cleaningtiktok'
    ,'emophase','mifamilia']
#for col in db.list_collection_names()[160:]:
for col in collist:
    print(col)
    for obj in db[col].find():
        if 'var_sb' not in obj['video_feature']['editing'].keys() or 'var_sb_c' not in obj['video_feature']['editing'].keys() or 'var_vgg' not in obj['video_feature']['editing'].keys() or 'var_vgg_c' not in obj['video_feature']['editing'].keys():
            vsb=0
            vvgg=0
            vsb_c=0
            vvgg_c=0
            img_list=list(obj['video_feature']['img_embed'].keys())
            for img_num in img_list:
                vvgg+=statistics.variance(obj['video_feature']['img_embed'][img_num][:1000])
                vsb+=statistics.variance(obj['video_feature']['img_embed'][img_num][1000:3096])
            if len(img_list)>1:

                for i in range(1000):
                    tempv=[obj['video_feature']['img_embed'][imgnum][i] for imgnum in img_list]
                    vvgg_c+=statistics.variance(tempv)
                for i in range(1000,3096):
                    tempv=[obj['video_feature']['img_embed'][imgnum][i] for imgnum in img_list]
                    vsb_c+=statistics.variance(tempv)
                vsb=vsb/len(img_list)
                vvgg = vvgg / len(img_list)
                vsb_c=vsb_c/1000
                vvgg_c=vvgg_c/2096
            db[col].update_one({'_id':obj['_id']},{'$set':{'video_feature.editing.var_sb':vsb,'video_feature.editing.var_vgg':vvgg,'video_feature.editing.var_sb_c':vsb_c,'video_feature.editing.var_vgg_c':vvgg_c}})